﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Configuration;
using System.Text;
using System.Threading;
using System.Net;
using System.Xml;
using SMS.PartnerUtils;
using SMS.ServiceManager;
using SMS.SmManager;
using digit.Core;

namespace SMS.ProcessUtils
{
    public class SmsCommon
    {
        private static int arraycup;
        public SmsCommon()
        { }

        #region MsgIn Process

        #region Process MO 80XX

        public static bool ProcessMessageMO_80(MsgIn_80Info objMsgIn80)
        {
            string strMessage = "";
            string str2 = "";
            string str3 = "";
            bool flagIn = false;
            MsgOut_80Info out1 = new MsgOut_80Info();

            try
            {
                string ctrlChar = "_";
                if (objMsgIn80 != null)
                {
                    // Format MessageIn 80 with "_"
                    strMessage = Utility.TrimAll(objMsgIn80.MsgContent.ToLower().Trim().Replace(".", "").Replace("/", ""), " ", ctrlChar);

                    #region Process MsgIn to Partner - 80

                    #endregion Process MsgIn to Partner - 80

                    int length = strMessage.LastIndexOf("_");
                    if (length > 0)
                    {
                        string str5 = strMessage.Substring(length + 1).Replace("O", "0").Replace("o", "0");
                        try
                        {
                            if (Validators.IsPhoneNumber(str5, PhoneNumberTypes.VN))
                            {
                                str3 = Validators.FormatMobileNo(str5);
                                strMessage = strMessage.Substring(0, length);
                            }
                        }
                        catch { }
                    }
                    ServiceDetailsInfo objServiceDetail = ServiceDetailsManager.VerifyForMsgOut_80(strMessage, objMsgIn80);
                    if (objServiceDetail != null)
                    {
                        if (objServiceDetail.ServiceID == objMsgIn80.ServiceID)
                        {
                            // Send other sms
                            if (Validators.IsPhoneNumber(str3, PhoneNumberTypes.International))
                            {
                                out1.RequestID = objMsgIn80.RequestID;
                                out1.RequestTime = objMsgIn80.RequestTime;
                                out1.ResponseTime = DateTime.Now;
                                out1.MsgInContent = str2;
                                out1.MsgInContent = strMessage;
                                out1.ContentType = 0;
                                out1.MsgInID = objMsgIn80.MsgInID;
                                out1.ServiceID = objMsgIn80.ServiceID;
                                out1.MsgIndex = 1;
                                out1.MsgTotal = 1;
                                out1.IsMore = 0;
                                out1.IsCDR = 0;
                                out1.CommandCode = objMsgIn80.MsgContent;
                                out1.Status = 0;

                                // Not support network
                                if ((((str3.Substring(0, 4) != "8490") && (str3.Substring(0, 4) != "8491")) && ((str3.Substring(0, 4) != "8498") && (str3.Substring(0, 4) != "8493"))) && (((str3.Substring(0, 4) != "8496") && (str3.Substring(0, 4) != "8495")) && (((str3.Substring(0, 4) != "8492") && (str3.Substring(0, 4) != "8494")) && (str3.Substring(0, 4) != "8497") && (str3.Substring(0, 3) != "841"))))
                                {
                                    str2 = "Hien tai chi cung cap dich vu cho mang (090,091,092,093,094,096,097,098,01)";
                                    out1.MsgType = 0;
                                    out1.ReceiverMobileNo = objMsgIn80.MobileNo;
                                    out1.MobileNo = str3;

                                    MsgOut_80Manager.Insert(out1);
                                    return true;
                                }
                                else
                                {
                                    // Send back message to owner
                                    str2 = CodeManager.GetContent("MESSAGE_SENDBACK", "0").Replace("#M", str3).Replace("#S", objMsgIn80.MsgContent.ToUpper());
                                    out1.MsgType = objServiceDetail.MsgType;
                                    out1.ReceiverMobileNo = str3;
                                    out1.MobileNo = objMsgIn80.MobileNo;

                                    MsgOut_80Manager.Insert(out1);
                                }
                            }

                            // Normal message
                            if (objServiceDetail.NumOfMsg > 5) objServiceDetail.NumOfMsg = 5;

                            byte insertedCnt = 0;
                            for (byte i = 1; i <= 5; i++)
                            {
                                out1 = new MsgOut_80Info();

                                // Send to other phone
                                if (Validators.IsPhoneNumber(str3, PhoneNumberTypes.International))
                                {
                                    out1.MsgType = 0;
                                    out1.MobileNo = str3;
                                    out1.ReceiverMobileNo = objMsgIn80.MobileNo;

                                    if ((objServiceDetail.ContentType == 0) || (objServiceDetail.ContentType == 8))
                                    {
                                        out1.MsgOutContent = "(Gui tu " + objMsgIn80.MobileNo + ")-";
                                    }

                                    out1.MsgOutContent = out1.MsgOutContent + objServiceDetail.Content1;
                                }
                                else
                                {
                                    // Message 1st - Nst
                                    out1.MobileNo = objMsgIn80.MobileNo;
                                    out1.ReceiverMobileNo = "";

                                    if (i == 1 && objServiceDetail.Content1 != null && objServiceDetail.Content1.Replace(" ", "") != "")
                                    {
                                        out1.MsgOutContent = objServiceDetail.Content1;
                                        insertedCnt++;
                                    }

                                    if (i == 2 && objServiceDetail.Content1 != null && objServiceDetail.Content2.Replace(" ", "") != "")
                                    {
                                        out1.MsgOutContent = objServiceDetail.Content2;
                                        insertedCnt++;
                                    }

                                    if (i == 3 && objServiceDetail.Content1 != null && objServiceDetail.Content3.Replace(" ", "") != "")
                                    {
                                        out1.MsgOutContent = objServiceDetail.Content3;
                                        insertedCnt++;
                                    }

                                    if (i == 4 && objServiceDetail.Content1 != null && objServiceDetail.Content4.Replace(" ", "") != "")
                                    {
                                        out1.MsgOutContent = objServiceDetail.Content4;
                                        insertedCnt++;
                                    }

                                    if (i == 5 && objServiceDetail.Content1 != null && objServiceDetail.Content5.Replace(" ", "") != "")
                                    {
                                        out1.MsgOutContent = objServiceDetail.Content5;
                                        insertedCnt++;
                                    }

                                    if ((out1.MsgOutContent.Replace(" ", "") == "") && (insertedCnt <= objServiceDetail.NumOfMsg)) continue;
                                }
                                out1.RequestTime = objMsgIn80.RequestTime;
                                out1.ResponseTime = DateTime.Now;
                                out1.RequestID = objMsgIn80.RequestID;
                                out1.MsgInContent = strMessage;
                                out1.ContentType = objServiceDetail.ContentType;
                                out1.MsgInID = objMsgIn80.MsgInID;
                                out1.ServiceID = objMsgIn80.ServiceID;
                                out1.MsgIndex = insertedCnt;
                                out1.MsgTotal = objServiceDetail.NumOfMsg;
                                out1.CommandCode = objServiceDetail.Cmd1;
                                out1.IsMore = (insertedCnt == objServiceDetail.NumOfMsg) ? (byte)0 : (byte)1;
                                out1.MsgType = (insertedCnt == objServiceDetail.NumOfMsg) ? (byte)1 : (byte)0;
                                out1.IsCDR = 0;
                                out1.Status = 0;

                                MsgOut_80Manager.Insert(out1);
                                out1 = null;
                            }
                        }
                        else
                        {
                            // Return wrong serviceid sms
                            string strWrongServiceID = "";

                            strWrongServiceID = CodeManager.GetContent("MESSAGE_ERROR", "0").Replace("#C", "DEC").Replace("#N", "8178");
                            if (strWrongServiceID != "")
                            {
                                out1.RequestID = objMsgIn80.RequestID;
                                out1.RequestTime = objMsgIn80.RequestTime;
                                out1.ResponseTime = DateTime.Now;
                                out1.MsgOutContent = strWrongServiceID;
                                out1.MsgInContent = strMessage;
                                out1.MsgInID = objMsgIn80.MsgInID;
                                out1.ReceiverMobileNo = str3;
                                out1.MobileNo = objMsgIn80.MobileNo;
                                out1.ServiceID = objMsgIn80.ServiceID;
                                out1.ContentType = 0;
                                out1.MsgType = 1;
                                out1.MsgIndex = 1;
                                out1.MsgTotal = 1;
                                out1.IsMore = 0;
                                out1.CommandCode = objMsgIn80.MsgContent;
                                out1.Status = 0;
                                out1.IsCDR = 0;
                                MsgOut_80Manager.Insert(out1);
                                out1 = null;
                            }
                        }
                    }
                    else if (CodeManager.GetCodeDetail("MESSAGE", "ERROR_STATUS").CodeValue == "1")
                    {
                        flagIn = true;
                    }
                    else
                    {
                        // Return wrong sms
                        string strWrongService = "";

                        strWrongService = CodeManager.GetContent("WRONGMSG", "0");
                        if (strWrongService != "")
                        {
                            out1.RequestID = objMsgIn80.RequestID;
                            out1.RequestTime = objMsgIn80.RequestTime;
                            out1.ResponseTime = DateTime.Now;
                            out1.MsgInContent = strMessage;
                            out1.MsgInID = objMsgIn80.MsgInID;
                            out1.MobileNo = objMsgIn80.MobileNo;
                            out1.RequestID = objMsgIn80.RequestID;
                            out1.ReceiverMobileNo = "";
                            out1.ServiceID = objMsgIn80.ServiceID;
                            out1.MsgOutContent = strWrongService;
                            out1.ContentType = 0;
                            out1.MsgType = 1;
                            out1.MsgIndex = 1;
                            out1.MsgTotal = 1;
                            out1.IsMore = 0;
                            out1.IsCDR = 0;
                            out1.CommandCode = objMsgIn80.MsgContent;
                            out1.Status = 0;
                            MsgOut_80Manager.Insert(out1);
                            out1 = null;
                        }
                    }
                    objServiceDetail = null;
                }

                if (flagIn)
                    MsgIn_80Manager.UpdateStatus(objMsgIn80.MsgInID, 4);

                else
                    MsgIn_80Manager.UpdateStatus(objMsgIn80.MsgInID, 9);

                return true;
            }
            catch
            {
                MsgIn_80Manager.UpdateStatus(objMsgIn80.MsgInID, 3);
                return false;
            }
        }

        #endregion Process MO 80XX

        #region Process MO 81XX

        public static bool ProcessMessageMO_81(MsgIn_81Info objMsgIn81)
        {
            string strMessage = "";
            string str = "";
            bool flagIn = false;
            MsgOut_81Info out1 = new MsgOut_81Info();

            try
            {
                string ctrlChar = "_";
                if (objMsgIn81 != null)
                {
                    // Format MessageIn 81XX with "_"
                    strMessage = Utility.TrimAll(objMsgIn81.MsgContent.ToLower().Trim().Replace(".", "").Replace("/", ""), " ", ctrlChar);

                    #region Process MsgIn to Partner - 81
                    try
                    {
                        string keyP = Util.GetAppSettings("PartnerAllServiceKey") != null ? Util.GetAppSettings("PartnerAllServiceKey").ToString().ToLower().Trim() : "";

                        string tempP = "";
                        if (objMsgIn81.MsgContent.IndexOf(" ") > 0)
                            tempP = objMsgIn81.MsgContent.Substring(0, objMsgIn81.MsgContent.IndexOf(" ")).ToLower().Trim();
                        else
                            tempP = objMsgIn81.MsgContent.ToLower();

                        // Check key if of partner
                        if (keyP.Contains(tempP))
                        {
                            PartnerProcess.ProcessSendMO(objMsgIn81.MsgInID, objMsgIn81.RequestID, objMsgIn81.MobileNo, objMsgIn81.ServiceID, objMsgIn81.MsgContent, tempP, objMsgIn81.RequestTime.ToString());

                            return true;
                        }
                    }
                    catch { return false; }
                    #endregion Process MsgIn to Partner - 81

                    int length = strMessage.LastIndexOf("_");
                    if (length > 0)
                    {
                        string str1 = strMessage.Substring(length + 1).Replace("O", "0").Replace("o", "0");
                        try
                        {
                            if (Validators.IsPhoneNumber(str1, PhoneNumberTypes.VN))
                            {
                                str = Validators.FormatMobileNo(str1);
                                strMessage = strMessage.Substring(0, length);
                            }
                        }
                        catch { }
                    }

                    ServiceDetailsInfo objServiceDetail = ServiceDetailsManager.VerifyForMsgOut_81(strMessage, objMsgIn81);
                    if (objServiceDetail != null)
                    {
                        if (objServiceDetail.ServiceID == "8278" || objServiceDetail.ServiceID == "8378" || objServiceDetail.ServiceID == objMsgIn81.ServiceID)
                        {
                            if (objServiceDetail.ServiceID == "8278" || objServiceDetail.ServiceID == "8378")
                            {
                                // Incoming ServiceID less than ServiceDetail

                                out1 = new MsgOut_81Info();

                                // One message only
                                out1.MobileNo = objMsgIn81.MobileNo;

                                if (objServiceDetail.Content1 != null && objServiceDetail.Content1.Replace(" ", "") != "")
                                    out1.MsgOutContent = objServiceDetail.Content1;

                                out1.ReceiverMobileNo = "";
                                out1.RequestTime = objMsgIn81.RequestTime;
                                out1.ResponseTime = DateTime.Now;
                                out1.RequestID = objMsgIn81.RequestID;
                                out1.MsgInContent = strMessage;
                                out1.ContentType = objServiceDetail.ContentType;
                                out1.MsgInID = objMsgIn81.MsgInID;
                                out1.ServiceID = objMsgIn81.ServiceID;
                                out1.MsgIndex = 1;
                                out1.MsgTotal = 1;
                                out1.CommandCode = objServiceDetail.Cmd1;
                                out1.IsMore = (byte)0;
                                out1.MsgType = (byte)1;
                                out1.IsCDR = 0;
                                out1.Status = 0;

                                MsgOut_81Manager.Insert(out1);
                                out1 = null;
                            }
                            else
                            {
                                // Normal message
                                if (objServiceDetail.NumOfMsg > 5) objServiceDetail.NumOfMsg = 5;

                                byte insertedCnt = 0;
                                for (byte i = 1; i <= 5; i++)
                                {
                                    out1 = new MsgOut_81Info();

                                    // Message 1st - Nst
                                    out1.MobileNo = objMsgIn81.MobileNo;
                                    out1.ReceiverMobileNo = "";

                                    if (i == 1 && objServiceDetail.Content1 != null && objServiceDetail.Content1.Replace(" ", "") != "")
                                    {
                                        out1.MsgOutContent = objServiceDetail.Content1;
                                        insertedCnt++;
                                    }

                                    if (i == 2 && objServiceDetail.Content1 != null && objServiceDetail.Content2.Replace(" ", "") != "")
                                    {
                                        out1.MsgOutContent = objServiceDetail.Content2;
                                        insertedCnt++;
                                    }

                                    if (i == 3 && objServiceDetail.Content1 != null && objServiceDetail.Content3.Replace(" ", "") != "")
                                    {
                                        out1.MsgOutContent = objServiceDetail.Content3;
                                        insertedCnt++;
                                    }

                                    if (i == 4 && objServiceDetail.Content1 != null && objServiceDetail.Content4.Replace(" ", "") != "")
                                    {
                                        out1.MsgOutContent = objServiceDetail.Content4;
                                        insertedCnt++;
                                    }

                                    if (i == 5 && objServiceDetail.Content1 != null && objServiceDetail.Content5.Replace(" ", "") != "")
                                    {
                                        out1.MsgOutContent = objServiceDetail.Content5;
                                        insertedCnt++;
                                    }

                                    if ((out1.MsgOutContent.Replace(" ", "") == "") && (insertedCnt <= objServiceDetail.NumOfMsg)) continue;

                                    out1.RequestTime = objMsgIn81.RequestTime;
                                    out1.ResponseTime = DateTime.Now;
                                    out1.RequestID = objMsgIn81.RequestID;
                                    out1.MsgInContent = strMessage;
                                    out1.ContentType = objServiceDetail.ContentType;
                                    out1.MsgInID = objMsgIn81.MsgInID;
                                    out1.ServiceID = objMsgIn81.ServiceID;
                                    out1.MsgIndex = insertedCnt;
                                    out1.MsgTotal = objServiceDetail.NumOfMsg;
                                    out1.CommandCode = objServiceDetail.Cmd1;
                                    out1.IsMore = (insertedCnt == objServiceDetail.NumOfMsg) ? (byte)0 : (byte)1;
                                    out1.MsgType = (insertedCnt == objServiceDetail.NumOfMsg) ? (byte)1 : (byte)0;
                                    out1.IsCDR = 0;
                                    out1.Status = 0;

                                    MsgOut_81Manager.Insert(out1);
                                    out1 = null;
                                }
                            }
                        }
                        else
                        {
                            // Wrong ServiceID - Move to Fix
                            flagIn = true;
                        }
                    }
                    else if (CodeManager.GetCodeDetail("MESSAGE", "ERROR_STATUS").CodeValue == "1")
                    {
                        flagIn = true;
                    }
                    else
                    {
                        // Return wrong sms
                        string strWrongService = "";

                        strWrongService = CodeManager.GetContent("WRONGMSG", "0");
                        if (strWrongService != "")
                        {
                            out1 = new MsgOut_81Info();

                            out1.RequestID = objMsgIn81.RequestID;
                            out1.RequestTime = objMsgIn81.RequestTime;
                            out1.ResponseTime = DateTime.Now;
                            out1.MsgInContent = strMessage;
                            out1.MsgInID = objMsgIn81.MsgInID;
                            out1.MobileNo = objMsgIn81.MobileNo;
                            out1.RequestID = objMsgIn81.RequestID;
                            out1.ReceiverMobileNo = "";
                            out1.ServiceID = objMsgIn81.ServiceID;
                            out1.MsgOutContent = strWrongService;
                            out1.ContentType = 0;
                            out1.MsgType = 1;
                            out1.MsgIndex = 1;
                            out1.MsgTotal = 1;
                            out1.IsMore = 0;
                            out1.IsCDR = 0;
                            out1.CommandCode = objMsgIn81.MsgContent;
                            out1.Status = 0;
                            MsgOut_81Manager.Insert(out1);
                            out1 = null;
                        }
                    }
                    objServiceDetail = null;
                }

                if (flagIn)
                    MsgIn_81Manager.UpdateStatus(objMsgIn81.MsgInID, 4);

                else
                    MsgIn_81Manager.UpdateStatus(objMsgIn81.MsgInID, 9);

                return true;
            }
            catch
            {
                MsgIn_81Manager.UpdateStatus(objMsgIn81.MsgInID, 3);
                return false;
            }
        }

        #endregion Process MO 81XX

        #region Process MO 82XX

        public static bool ProcessMessageMO_82(MsgIn_82Info objMsgIn82)
        {
            string strMessage = "";
            string str = "";
            bool flagIn = false;
            string result_82 = "";
            string tt = "";
            string strt1= "";
            string strt2 = "";
            string strt3 = "";
            
            MsgOut_82Info out1 = new MsgOut_82Info();

            try
            {
                string ctrlChar = "_";
                if (objMsgIn82 != null)
                {
                    // Format MessageIn 82XX with "_"
                    strMessage = Utility.TrimAll(objMsgIn82.MsgContent.ToLower().Trim().Replace(".", "").Replace("/", ""), " ", ctrlChar);

                    #region Process MsgIn to Partner - 82

                    #endregion Process MsgIn to Partner - 82

                    int length = strMessage.LastIndexOf("_");
                    if (length > 0)
                    {
                        string str1 = strMessage.Substring(length + 1).Replace("O", "0").Replace("o", "0");
                        try
                        {
                            if (Validators.IsPhoneNumber(str1, PhoneNumberTypes.VN))
                            {
                                str = Validators.FormatMobileNo(str1);
                                strMessage = strMessage.Substring(0, length);
                            }
                        }
                        catch { }
                    }
                    ServiceDetailsInfo objServiceDetail = ServiceDetailsManager.VerifyForMsgOut_82(strMessage, objMsgIn82);
                    if (objServiceDetail != null)
                    {
                       // if(objServiceDetail.Param1)
                        if (objServiceDetail.ServiceID != "8678" && objServiceDetail.ServiceID != "8778")
                        {
                            // Normal message
                            if (objServiceDetail.NumOfMsg > 5) objServiceDetail.NumOfMsg = 5;
                                                                                 
                            byte insertedCnt = 0;

                            #region xu ly Auto
                            
                            if (objServiceDetail.Content1.IndexOf("[auto") != -1)
                            {
                                byte tinnhantieptheo = 0;
                                string quangcao = strMessage;
                                string quangcaodau = "De xem tiep soan: ";
                               // int sotrang;
                                for (byte i = 1; i <= 2; i++)
                                {
                                    out1 = new MsgOut_82Info();

                                    // Message 1st - Nst
                                    out1.MobileNo = objMsgIn82.MobileNo;
                                    out1.ReceiverMobileNo = "";  
                                                       
                                    if (i == 1 && objServiceDetail.Content1 != null)
                                    {
                                        if (objServiceDetail.Content1.IndexOf("[auto]") != -1)
                                        {
                                            string[] array = objServiceDetail.Content1.Split(',');
                                            //// tường thuật
                                            if (array.Length == 3)
                                            {
                                                tt = Utility.GetMsgStockFromURL("http://123.30.189.186:8086/KetQuaTranDau.aspx?id=" + array[1] + "&bds=" + array[2]);
                                                string[] arr = tt.Split('|');
                                                result_82 = arr[0];
                                                if (arr.Length >= 2)
                                                    tinnhantieptheo = 1;
                                                else
                                                    tinnhantieptheo = 0;
                                            }      
                                            /*
                                            if (array.Length == 5)
                                            {
                                                string p = "";
                                                if (Utility.IsNumeric(array[4]))
                                                    p = array[4];
                                                if (int.Parse(p) > 0)
                                                    quangcao = strMessage.Substring(0, strMessage.Length - 1);
                                                sotrang = int.Parse(p) + 1;
                                                result_82 = Utility.GetMsgStockFromURL("http://123.30.189.186:8086/KetQuaGiaiDau.aspx?l=" + array[1] + "&r=" + array[2] + "&s=" + array[3] + "&p=" + p) + quangcaodau + quangcao.Replace("_", " ") + sotrang.ToString();
                                                if (result_82.Length >= 160)
                                                    tinnhantieptheo = 1;
                                                else tinnhantieptheo = 0;
                                            }
                                             */
                                            if (array.Length == 6)
                                            {
                                            //    string chuoi = "";
                                                string p = "";
                                                if (Utility.IsNumeric(array[4]))
                                                    p = array[4];
                                                if (array[5].ToLower() == "k")
                                                {
                                                    result_82 = Utility.GetMsgStockFromURL("http://123.30.189.186:8086/KetQuaGiaiDau.aspx?l=" + array[1] + "&r=" + array[2] + "&s=" + array[3] + "&p=" + p);
                                                    result_82 = result_82.Trim().Replace("    ", " ").Replace("   ", " ").Replace("  ", " ");
                                                }
                                                if (array[5].ToLower().IndexOf("t") != -1)
                                                {
                                                    string tinnoiqc = array[5].ToLower().Replace("t", "").Trim();
                                                    if (tinnoiqc == "1" || tinnoiqc == "a")
                                                    {
                                                        quangcao = strMessage;
                                                    }
                                                    else
                                                    {
                                                        quangcao = strMessage.Substring(0, strMessage.Length - 1);
                                                    }
                                                    //sotrang = int.Parse(p) + 1;                                                    
                                                    result_82 = Utility.GetMsgStockFromURL("http://123.30.189.186:8086/KetQuaGiaiDau.aspx?l=" + array[1] + "&r=" + array[2] + "&s=" + array[3] + "&p=" + p) + quangcaodau + quangcao.Replace("_", " ") + tinnoiqc.ToUpper();
                                                    result_82 = result_82.Trim().Replace("    ", " ").Replace("   ", " ").Replace("  ", " ");
                                                }                                               
                                                // check xem con gui tin nhan tiep theo ko                                                
                                                if (result_82.Length >= 160)
                                                {
                                                    //strt1 = result_82.Substring(160);
                                                    //result_82 = result_82.Substring(0, 160);   
                                                    string tempt = result_82;
                                                    result_82 = Utility.CatChuoi(result_82, 160);
                                                    strt1 = tempt.Substring(result_82.Length);
                                                    tinnhantieptheo = 1;
                                                }
                                                else tinnhantieptheo = 0;
                                            }
                                            out1.MsgOutContent = result_82.ToString();
                                        }
                                        if (objServiceDetail.Content1.IndexOf("[autocup]") != -1)
                                        {
                                            string[] arraycup = objServiceDetail.Content1.Split(',');   
                                            /*
                                            if (arraycup.Length == 4)
                                            {
                                                string p = "";

                                                if (Utility.IsNumeric(arraycup[3]))
                                                    p = arraycup[3];
                                                if (int.Parse(p) > 0)
                                                    quangcao = strMessage.Substring(0, strMessage.Length - 1);
                                                sotrang = int.Parse(p) + 1;
                                                result_82 = Utility.GetMsgStockFromURL("http://123.30.189.186:8086/KetQuaCup.aspx?l=" + arraycup[1] + "&s=" + arraycup[2] + "&p=" + p) + quangcaodau + quangcao.Replace("_", " ") + sotrang.ToString();
                                                if (result_82.Length >= 160)
                                                    tinnhantieptheo = 1;
                                                else tinnhantieptheo = 0;
                                            }
                                             */
                                            if (arraycup.Length == 5)
                                            {
                                                string p = "";

                                                if (Utility.IsNumeric(arraycup[3]))
                                                    p = arraycup[3];
                                                // truong hop ko con tin nhan tiep theo. k cho hien quang cao.
                                                if (arraycup[4].ToLower() == "k")
                                                {
                                                    result_82 = Utility.GetMsgStockFromURL("http://123.30.189.186:8086/KetQuaCup.aspx?l=" + arraycup[1] + "&s=" + arraycup[2] + "&p=" + p);
                                                    result_82 = result_82.Trim().Replace("    ", " ").Replace("   ", " ").Replace("  ", " ");
                                                }
                                                if (arraycup[4].ToLower().IndexOf("t") != -1)
                                                {
                                                    string tinnoiqc = arraycup[4].ToLower().Replace("t", "").Trim();                                                
                                                    if (int.Parse(p) > 0)
                                                        quangcao = strMessage.Substring(0, strMessage.Length - 1);
                                               //     sotrang = int.Parse(p) + 1;
                                                    result_82 = Utility.GetMsgStockFromURL("http://123.30.189.186:8086/KetQuaCup.aspx?l=" + arraycup[1] + "&s=" + arraycup[2] + "&p=" + p) + quangcaodau + quangcao.Replace("_", " ") + tinnoiqc.ToUpper();
                                                    result_82 = result_82.Trim().Replace("    ", " ").Replace("   ", " ").Replace("  ", " ");
                                                }

                                                if (result_82.Length >= 160)
                                                {
                                                    //strt1 = result_82.Substring(160);
                                                    //result_82 = result_82.Substring(0, 160);   
                                                    string tempt = result_82;
                                                    result_82 = Utility.CatChuoi(result_82, 160);
                                                    strt1 = tempt.Substring(result_82.Length);
                                                                         
                                                    tinnhantieptheo = 1;
                                                }
                                                else tinnhantieptheo = 0;
                                            }
                                            out1.MsgOutContent = result_82.ToString();
                                        }
                                        insertedCnt++;
                                    }

                                    if (i == 2 && tinnhantieptheo == 1 && strt1.Replace(" ", "") != "" )
                                    {
                                        if (objServiceDetail.Content1.IndexOf("[auto]") != -1)
                                        {
                                            string[] array = objServiceDetail.Content1.Split(',');

                                            if (array.Length == 3)
                                            {                                        
                                                string[] arr = tt.Split('|');
                                                result_82 = arr[1];
                                                if (arr.Length >= 3)
                                                    tinnhantieptheo = 1;
                                                else
                                                    tinnhantieptheo = 0;
                                            }      
                                            /*
                                            if (array.Length == 5)
                                            {                                          
                                                result_82 = result_82.Substring(160);
                                                if (result_82.Length >= 160)
                                                    tinnhantieptheo = 1;
                                                else tinnhantieptheo = 0;
                                            }
                                             */
                                            if (array.Length == 6)
                                            {
                                                //// remote ky tu ; o truoc
                                                ////result_82 = strt1.Remove(0,1).Trim();
                                                result_82 = strt1.TrimStart(';').Trim();
                                                if (result_82.Length >= 160)
                                                {
                                                    strt2 = result_82.Substring(160);
                                                    result_82 = result_82.Substring(0, 160);                                                    
                                                    tinnhantieptheo = 1;
                                                }
                                                else tinnhantieptheo = 0;
                                            }
                                            out1.MsgOutContent = result_82.ToString();
                                        }
                                        if (objServiceDetail.Content1.IndexOf("[autocup]") != -1)
                                        {
                                            string[] arraycup = objServiceDetail.Content1.Split(',');
                                            /*
                                            if (arraycup.Length == 4)
                                            {                                         
                                                result_82 = result_82.Substring(160);
                                                if (result_82.Length >= 160)
                                                    tinnhantieptheo = 1;
                                                else tinnhantieptheo = 0;
                                            }
                                             */
                                            if (arraycup.Length == 5)
                                            {
                                                ////result_82 = strt1.Remove(0,1).Trim();
                                                result_82 = strt1.TrimStart(';').Trim();                                            
                                                if (result_82.Length >= 160)
                                                {
                                                    strt2 = result_82.Substring(160);
                                                    result_82 = result_82.Substring(0, 160);  
                                                    tinnhantieptheo = 1;
                                                }
                                                else tinnhantieptheo = 0;
                                            }
                                            out1.MsgOutContent = result_82.ToString();
                                        }

                                        insertedCnt++;
                                    }
                                    /*
                                    if (i == 3 && tinnhantieptheo == 1)
                                    {
                                        if (objServiceDetail.Content1.IndexOf("[auto]") != -1)
                                        {
                                            string[] array = objServiceDetail.Content1.Split(',');

                                            if (array.Length == 3)
                                            {                          
                                                string[] arr = tt.Split('|');
                                                result_82 = arr[2];
                                                if (arr.Length >= 4)
                                                    tinnhantieptheo = 1;
                                                else
                                                    tinnhantieptheo = 0;
                                            }                                            
                                            if (array.Length == 5)
                                            {                                         
                                                result_82 = result_82.Substring(160);
                                                if (result_82.Length >= 160)
                                                    tinnhantieptheo = 1;
                                                else tinnhantieptheo = 0;
                                            }
                                            if (array.Length == 6)
                                            {
                                                result_82 = strt2;
                                                if (result_82.Length >= 160)
                                                {
                                                    strt3 = result_82.Substring(160);
                                                    result_82 = result_82.Substring(0, 160);  
                                                    tinnhantieptheo = 1;
                                                }
                                                else tinnhantieptheo = 0;
                                            }
                                            out1.MsgOutContent = result_82.ToString();
                                        }
                                        if (objServiceDetail.Content1.IndexOf("[autocup]") != -1)
                                        {
                                            string[] arraycup = objServiceDetail.Content1.Split(',');
                                            
                                            if (arraycup.Length == 4)
                                            {                                             
                                                result_82 = result_82.Substring(160);
                                                if (result_82.Length >= 160)
                                                    tinnhantieptheo = 1;
                                                else tinnhantieptheo = 0;
                                            }
                                            if (arraycup.Length == 5)
                                            {
                                                result_82 = strt2;
                                                if (result_82.Length >= 160)
                                                {
                                                    strt3 = result_82.Substring(160);
                                                    result_82 = result_82.Substring(0, 160);                                                    
                                                    tinnhantieptheo = 1;
                                                }
                                                else tinnhantieptheo = 0;
                                            }
                                            out1.MsgOutContent = result_82.ToString();
                                        }                                       

                                        insertedCnt++;
                                    }
                                    if (i == 4 && objServiceDetail.Content4 != null && objServiceDetail.Content4.Replace(" ", "") != "")
                                    {
                                        if (objServiceDetail.Content1.IndexOf("[auto]") != -1)
                                        {
                                            string[] array = objServiceDetail.Content1.Split(',');

                                            if (array.Length == 3)
                                            {                                            
                                                string[] arr = tt.Split('|');
                                                result_82 = arr[3];
                                                if (arr.Length >= 5)
                                                    tinnhantieptheo = 1;
                                                else tinnhantieptheo = 0;
                                            }
                                            
                                            if (array.Length == 5)
                                            {                                               
                                                result_82 = result_82.Substring(160);
                                                if (result_82.Length >= 160)
                                                    tinnhantieptheo = 1;
                                                else tinnhantieptheo = 0;
                                            }
                                            if (array.Length == 6)
                                            {                                          
                                                result_82 = result_82.Substring(160);
                                                if (result_82.Length >= 160)
                                                    tinnhantieptheo = 1;
                                                else tinnhantieptheo = 0;
                                            }
                                            out1.MsgOutContent = result_82.ToString();
                                        }
                                        if (objServiceDetail.Content1.IndexOf("[autocup]") != -1)
                                        {
                                            string[] arraycup = objServiceDetail.Content1.Split(',');
                                             
                                            if (arraycup.Length == 4)
                                            {                                               
                                                result_82 = result_82.Substring(160);
                                                if (result_82.Length >= 160)
                                                    tinnhantieptheo = 1;
                                                else tinnhantieptheo = 0;
                                            }
                                              if (arraycup.Length == 5)
                                            {                                         
                                                result_82 = result_82.Substring(160);
                                                if (result_82.Length >= 160)
                                                    tinnhantieptheo = 1;
                                                else tinnhantieptheo = 0;
                                            }
                                            out1.MsgOutContent = result_82.ToString();
                                        }                                       

                                        insertedCnt++;
                                    }

                                    if (i == 5 && objServiceDetail.Content5 != null && objServiceDetail.Content5.Replace(" ", "") != "")
                                    {
                                        if (objServiceDetail.Content1.IndexOf("[auto]") != -1)
                                        {
                                            string[] array = objServiceDetail.Content1.Split(',');

                                            if (array.Length == 3)
                                            {                                          
                                                string[] arr = tt.Split('|');
                                                result_82 = arr[4];
                                                if (arr.Length >= 6)
                                                    tinnhantieptheo = 1;
                                                else tinnhantieptheo = 0;
                                            }                                            
                                            if (array.Length == 5)
                                            {                                               
                                                result_82 = result_82.Substring(160);
                                                if (result_82.Length >= 160)
                                                    tinnhantieptheo = 1;
                                                else tinnhantieptheo = 0;
                                            }
                                            if (array.Length == 6)
                                            {                                          
                                                result_82 = result_82.Substring(160);
                                                if (result_82.Length >= 160)
                                                    tinnhantieptheo = 1;
                                                else tinnhantieptheo = 0;
                                            }

                                            out1.MsgOutContent = result_82.ToString();
                                        }
                                        if (objServiceDetail.Content1.IndexOf("[autocup]") != -1)
                                        {
                                            string[] arraycup = objServiceDetail.Content1.Split(',');
                                            
                                            if (arraycup.Length == 4)
                                            {                                      
                                                result_82 = result_82.Substring(160);
                                                if (result_82.Length >= 160)
                                                    tinnhantieptheo = 1;
                                                else tinnhantieptheo = 0;
                                            }
                                            if (arraycup.Length == 5)
                                            {                                         
                                                result_82 = result_82.Substring(160);
                                                if (result_82.Length >= 160)
                                                    tinnhantieptheo = 1;
                                                else tinnhantieptheo = 0;
                                            }
                                            out1.MsgOutContent = result_82.ToString();
                                        }                                      

                                        insertedCnt++;
                                    }
                                    */
                                    if ((out1.MsgOutContent.Replace(" ", "") == "") && (insertedCnt <= objServiceDetail.NumOfMsg)) continue;

                                    out1.RequestTime = objMsgIn82.RequestTime;
                                    out1.ResponseTime = DateTime.Now;
                                    out1.RequestID = objMsgIn82.RequestID;
                                    out1.MsgInContent = strMessage;
                                    out1.ContentType = objServiceDetail.ContentType;
                                    out1.MsgInID = objMsgIn82.MsgInID;
                                    out1.ServiceID = objMsgIn82.ServiceID;
                                    out1.MsgIndex = insertedCnt;
                                    out1.MsgTotal = objServiceDetail.NumOfMsg;
                                    out1.CommandCode = objServiceDetail.Cmd1;
                                    out1.IsMore = (insertedCnt == objServiceDetail.NumOfMsg) ? (byte)0 : (byte)1;
                                    out1.MsgType = (insertedCnt == objServiceDetail.NumOfMsg) ? (byte)1 : (byte)0;
                                    out1.IsCDR = 0;
                                    out1.Status = 0;

                                    MsgOut_82Manager.Insert(out1);
                                    out1 = null;
                                }
                            }
                            #endregion


                            #region Xy ly nhap tay cu

                            // Check live result: #1, #2, #3
                            else
                            {
                                string rep1 = "";
                                string rep2 = "";
                                string rep3 = "";

                                if (!Util.IsNull(objServiceDetail.Timer1))
                                    rep1 = Utility.GetMinuteString(objServiceDetail.Timer1);

                                if (!Util.IsNull(objServiceDetail.Timer2))
                                    rep2 = Utility.GetMinuteString(objServiceDetail.Timer2);

                                if (!Util.IsNull(objServiceDetail.Timer3))
                                    rep3 = Utility.GetMinuteString(objServiceDetail.Timer3);
                                for (byte i = 1; i <= 5; i++)
                                {
                                    out1 = new MsgOut_82Info();

                                    // Message 1st - Nst
                                    out1.MobileNo = objMsgIn82.MobileNo;
                                    out1.ReceiverMobileNo = "";

                                    if (i == 1 && objServiceDetail.Content1 != null && objServiceDetail.Content1.Replace(" ", "") != "")
                                    {
                                        out1.MsgOutContent = objServiceDetail.Content1.Replace("#1", rep1).Replace("#2", rep2).Replace("#3", rep3);
                                        insertedCnt++;
                                    }

                                    if (i == 2 && objServiceDetail.Content2 != null && objServiceDetail.Content2.Replace(" ", "") != "")
                                    {
                                        out1.MsgOutContent = objServiceDetail.Content2.Replace("#1", rep1).Replace("#2", rep2).Replace("#3", rep3);
                                        insertedCnt++;
                                    }

                                    if (i == 3 && objServiceDetail.Content3 != null && objServiceDetail.Content3.Replace(" ", "") != "")
                                    {
                                        out1.MsgOutContent = objServiceDetail.Content3.Replace("#1", rep1).Replace("#2", rep2).Replace("#3", rep3);
                                        insertedCnt++;
                                    }

                                    if (i == 4 && objServiceDetail.Content4 != null && objServiceDetail.Content4.Replace(" ", "") != "")
                                    {
                                        out1.MsgOutContent = objServiceDetail.Content4.Replace("#1", rep1).Replace("#2", rep2).Replace("#3", rep3);
                                        insertedCnt++;
                                    }

                                    if (i == 5 && objServiceDetail.Content5 != null && objServiceDetail.Content5.Replace(" ", "") != "")
                                    {
                                        out1.MsgOutContent = objServiceDetail.Content5;
                                        insertedCnt++;
                                    }

                                    if ((out1.MsgOutContent.Replace(" ", "") == "") && (insertedCnt <= objServiceDetail.NumOfMsg)) continue;

                                    out1.RequestTime = objMsgIn82.RequestTime;
                                    out1.ResponseTime = DateTime.Now;
                                    out1.RequestID = objMsgIn82.RequestID;
                                    out1.MsgInContent = strMessage;
                                    out1.ContentType = objServiceDetail.ContentType;
                                    out1.MsgInID = objMsgIn82.MsgInID;
                                    out1.ServiceID = objMsgIn82.ServiceID;
                                    out1.MsgIndex = insertedCnt;
                                    out1.MsgTotal = objServiceDetail.NumOfMsg;
                                    out1.CommandCode = objServiceDetail.Cmd1;
                                    out1.IsMore = (insertedCnt == objServiceDetail.NumOfMsg) ? (byte)0 : (byte)1;
                                    out1.MsgType = (insertedCnt == objServiceDetail.NumOfMsg) ? (byte)1 : (byte)0;
                                    out1.IsCDR = 0;
                                    out1.Status = 0;

                                    MsgOut_82Manager.Insert(out1);
                                    out1 = null;
                                }
                            }
                            #endregion
                        }
                        else
                        {
                            // Wrong ServiceID - Move to Fix
                            flagIn = true;
                        }
                    }
                    else if (CodeManager.GetCodeDetail("MESSAGE", "ERROR_STATUS").CodeValue == "1") //// trạng thái hệ thống sửa lỗi tin nhắn
                    {
                        // flagIn = true; de tu động sửa code

                        // thinh code them
                        string strtinnhaguiKH = "";
                        string strtinnhanError = "Quy khach da nhan sai cu phap cua he thong. Gui tin den 8278 de nhan dich vu he thong trong ngay: ";
                        if (strMessage.ToLower().IndexOf("dec_kq") != -1)
                            strtinnhaguiKH = strtinnhanError + "DEC KQ TODAY => Lich ket qua";
                        else if (strMessage.ToLower().IndexOf("dec_tt") != -1)
                            strtinnhaguiKH = strtinnhanError + "DEC TT TODAY => Tuong Thuat";
                        else if (strMessage.ToLower().IndexOf("dec_dh") != -1)
                            strtinnhaguiKH = strtinnhanError + "DEC DH TODAY=> Doi Hinh";
                        else if (strMessage.ToLower().IndexOf("dec_w") != -1)
                            strtinnhaguiKH = strtinnhanError + "DEC W TODAY => Tip bong da";
                        else if (strMessage.ToLower().IndexOf("dec_tl") != -1)
                            strtinnhaguiKH = strtinnhanError + "DEC TL TODAY => Ty Le";
                        else if (strMessage.ToLower().IndexOf("dec_ltd") != -1)
                            strtinnhaguiKH = strtinnhanError + "DEC LTD TODAY => Lich thi dau";
                        else
                            strtinnhaguiKH = strtinnhanError + "DEC TODAY ";


                        out1 = new MsgOut_82Info();

                        out1.RequestID = objMsgIn82.RequestID;
                        out1.RequestTime = objMsgIn82.RequestTime;
                        out1.ResponseTime = DateTime.Now;
                        out1.MsgInContent = strMessage;
                        out1.MsgInID = objMsgIn82.MsgInID;
                        out1.MobileNo = objMsgIn82.MobileNo;
                        out1.RequestID = objMsgIn82.RequestID;
                        out1.ReceiverMobileNo = "";
                        out1.ServiceID = objMsgIn82.ServiceID;
                        out1.MsgOutContent = strtinnhaguiKH;
                        out1.ContentType = 0;
                        out1.MsgType = 0; // MsgType = 0 khong tinh cdr
                        out1.MsgIndex = 1;
                        out1.MsgTotal = 1;
                        out1.IsMore = 0;
                        out1.IsCDR = 0;
                        out1.CommandCode = objMsgIn82.MsgContent;
                        out1.Status = 0;
                        MsgOut_82Manager.Insert(out1);
                        out1 = null; 
                    }
                    else
                    {
                        // Return wrong sms
                        string strWrongService = "";

                        strWrongService = CodeManager.GetContent("WRONGMSG", "0");
                        if (strWrongService != "")
                        {
                            out1 = new MsgOut_82Info();

                            out1.RequestID = objMsgIn82.RequestID;
                            out1.RequestTime = objMsgIn82.RequestTime;
                            out1.ResponseTime = DateTime.Now;
                            out1.MsgInContent = strMessage;
                            out1.MsgInID = objMsgIn82.MsgInID;
                            out1.MobileNo = objMsgIn82.MobileNo;
                            out1.RequestID = objMsgIn82.RequestID;
                            out1.ReceiverMobileNo = "";
                            out1.ServiceID = objMsgIn82.ServiceID;
                            out1.MsgOutContent = strWrongService;
                            out1.ContentType = 0;
                            out1.MsgType = 1;
                            out1.MsgIndex = 1;
                            out1.MsgTotal = 1;
                            out1.IsMore = 0;
                            out1.IsCDR = 0;
                            out1.CommandCode = objMsgIn82.MsgContent;
                            out1.Status = 0;
                            MsgOut_82Manager.Insert(out1);
                            out1 = null;
                        }                      

                    }
                    objServiceDetail = null;
                }

                if (flagIn)
                    MsgIn_82Manager.UpdateStatus(objMsgIn82.MsgInID, 4);

                else
                    MsgIn_82Manager.UpdateStatus(objMsgIn82.MsgInID, 9);

                return true;
            }
            catch(Exception ex)
            {
                MsgIn_82Manager.UpdateStatus(objMsgIn82.MsgInID, 3);

                // Write log to WindowLogs
                string sSource;
                string sLog;
                string sEvent;

                sSource = "8x78 SMS";
                sLog = "Application";
                sEvent = ex.Message;

                if (!EventLog.SourceExists(sSource))
                    EventLog.CreateEventSource(sSource, sLog);

                EventLog.WriteEntry(sSource, sEvent);
                EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Warning, 234);
                //End write log ro WindowLogs

                return false;
            }
        }

        #endregion Process MO 82XX

        #region Process MO 83XX

        public static bool ProcessMessageMO_83(MsgIn_83Info objMsgIn83)
        {
            string strMessage = "";
            string str = "";
            bool flagIn = false;
            MsgOut_83Info out1 = new MsgOut_83Info();

            try
            {
                string ctrlChar = "_";
                if (objMsgIn83 != null)
                {
                    // Format MessageIn 83XX with "_"
                    strMessage = Utility.TrimAll(objMsgIn83.MsgContent.ToLower().Trim().Replace(".", "").Replace("/", ""), " ", ctrlChar);

                    #region Process MsgIn to Partner - 83

                    #endregion Process MsgIn to Partner - 83

                    int length = strMessage.LastIndexOf("_");
                    if (length > 0)
                    {
                        string str1 = strMessage.Substring(length + 1).Replace("O", "0").Replace("o", "0");
                        try
                        {
                            if (Validators.IsPhoneNumber(str1, PhoneNumberTypes.VN))
                            {
                                str = Validators.FormatMobileNo(str1);
                                strMessage = strMessage.Substring(0, length);
                            }
                        }
                        catch { }
                    }
                    ServiceDetailsInfo objServiceDetail = ServiceDetailsManager.VerifyForMsgOut_83(strMessage, objMsgIn83);
                    if (objServiceDetail != null)
                    {
                        if (objServiceDetail.ServiceID.Trim() != "8678" && objServiceDetail.ServiceID.Trim() != "8778")
                        {
                            // Normal message
                            if (objServiceDetail.NumOfMsg > 5) objServiceDetail.NumOfMsg = 5;

                            byte insertedCnt = 0;
                            for (byte i = 1; i <= 5; i++)
                            {
                                out1 = new MsgOut_83Info();

                                // Message 1st - Nst
                                out1.MobileNo = objMsgIn83.MobileNo;
                                out1.ReceiverMobileNo = "";

                                if (i == 1 && objServiceDetail.Content1 != null && objServiceDetail.Content1.Replace(" ", "") != "")
                                {
                                    out1.MsgOutContent = objServiceDetail.Content1;
                                    insertedCnt++;
                                }

                                if (i == 2 && objServiceDetail.Content1 != null && objServiceDetail.Content2.Replace(" ", "") != "")
                                {
                                    out1.MsgOutContent = objServiceDetail.Content2;
                                    insertedCnt++;
                                }

                                if (i == 3 && objServiceDetail.Content1 != null && objServiceDetail.Content3.Replace(" ", "") != "")
                                {
                                    out1.MsgOutContent = objServiceDetail.Content3;
                                    insertedCnt++;
                                }

                                if (i == 4 && objServiceDetail.Content1 != null && objServiceDetail.Content4.Replace(" ", "") != "")
                                {
                                    out1.MsgOutContent = objServiceDetail.Content4;
                                    insertedCnt++;
                                }

                                if (i == 5 && objServiceDetail.Content1 != null && objServiceDetail.Content5.Replace(" ", "") != "")
                                {
                                    out1.MsgOutContent = objServiceDetail.Content5;
                                    insertedCnt++;
                                }

                                if ((out1.MsgOutContent.Replace(" ", "") == "") && (insertedCnt <= objServiceDetail.NumOfMsg)) continue;
                                
                                out1.RequestTime = objMsgIn83.RequestTime;
                                out1.ResponseTime = DateTime.Now;
                                out1.RequestID = objMsgIn83.RequestID;
                                out1.MsgInContent = strMessage;
                                out1.ContentType = objServiceDetail.ContentType;
                                out1.MsgInID = objMsgIn83.MsgInID;
                                out1.ServiceID = objMsgIn83.ServiceID;
                                out1.MsgIndex = insertedCnt;
                                out1.MsgTotal = objServiceDetail.NumOfMsg;
                                out1.CommandCode = objServiceDetail.Cmd1;
                                out1.IsMore = (insertedCnt == objServiceDetail.NumOfMsg) ? (byte)0 : (byte)1;
                                out1.MsgType = (insertedCnt == objServiceDetail.NumOfMsg) ? (byte)1 : (byte)0;
                                out1.IsCDR = 0;
                                out1.Status = 0;

                                MsgOut_83Manager.Insert(out1);
                            }
                        }
                        else
                        {
                            // Wrong ServiceID - Move to Fix
                            flagIn = true;
                        }
                    }
                    else if (CodeManager.GetCodeDetail("MESSAGE", "ERROR_STATUS").CodeValue == "1")
                    {
                        flagIn = true;
                    }
                    else
                    {
                        // Return wrong sms
                        string strWrongService = "";

                        strWrongService = CodeManager.GetContent("WRONGMSG", "0");
                        if (strWrongService != "")
                        {
                            out1 = new MsgOut_83Info();

                            out1.RequestID = objMsgIn83.RequestID;
                            out1.RequestTime = objMsgIn83.RequestTime;
                            out1.ResponseTime = DateTime.Now;
                            out1.MsgInContent = strMessage;
                            out1.MsgInID = objMsgIn83.MsgInID;
                            out1.MobileNo = objMsgIn83.MobileNo;
                            out1.RequestID = objMsgIn83.RequestID;
                            out1.ReceiverMobileNo = "";
                            out1.ServiceID = objMsgIn83.ServiceID;
                            out1.MsgOutContent = strWrongService;
                            out1.ContentType = 0;
                            out1.MsgType = 1;
                            out1.MsgIndex = 1;
                            out1.MsgTotal = 1;
                            out1.IsMore = 0;
                            out1.IsCDR = 0;
                            out1.CommandCode = objMsgIn83.MsgContent;
                            out1.Status = 0;
                            MsgOut_83Manager.Insert(out1);
                            out1 = null;
                        }
                    }
                    objServiceDetail = null;
                }

                if (flagIn)
                    MsgIn_83Manager.UpdateStatus(objMsgIn83.MsgInID, 4);

                else
                    MsgIn_83Manager.UpdateStatus(objMsgIn83.MsgInID, 9);

                return true;
            }
            catch
            {
                MsgIn_83Manager.UpdateStatus(objMsgIn83.MsgInID, 3);
                return false;
            }
        }

        #endregion Process MO 83XX

        #region Process MO 84XX

        public static bool ProcessMessageMO_84(MsgIn_84Info objMsgIn84)
        {
            string strMessage = "";
            string str = "";
            bool flagIn = false;
            MsgOut_84Info out1 = new MsgOut_84Info();

            try
            {
                string ctrlChar = "_";
                if (objMsgIn84 != null)
                {
                    // Format MessageIn 84XX with "_"
                    strMessage = Utility.TrimAll(objMsgIn84.MsgContent.ToLower().Trim().Replace(".", "").Replace("/", ""), " ", ctrlChar);

                    #region Process MsgIn to Partner - 84

                    #endregion Process MsgIn to Partner - 84

                    int length = strMessage.LastIndexOf("_");
                    if (length > 0)
                    {
                        string str1 = strMessage.Substring(length + 1).Replace("O", "0").Replace("o", "0");
                        try
                        {
                            if (Validators.IsPhoneNumber(str1, PhoneNumberTypes.VN))
                            {
                                str = Validators.FormatMobileNo(str1);
                                strMessage = strMessage.Substring(0, length);
                            }
                        }
                        catch { }
                    }
                    ServiceDetailsInfo objServiceDetail = ServiceDetailsManager.VerifyForMsgOut_84(strMessage, objMsgIn84);
                    if (objServiceDetail != null)
                    {
                        if (objServiceDetail.ServiceID.Trim() != "8678" && objServiceDetail.ServiceID.Trim() != "8778")
                        {
                            // Normal message
                            if (objServiceDetail.NumOfMsg > 5) objServiceDetail.NumOfMsg = 5;

                            byte insertedCnt = 0;
                            for (byte i = 1; i <= 5; i++)
                            {
                                out1 = new MsgOut_84Info();

                                // Message 1st - Nst
                                out1.MobileNo = objMsgIn84.MobileNo;
                                out1.ReceiverMobileNo = "";

                                if (i == 1 && objServiceDetail.Content1 != null && objServiceDetail.Content1.Replace(" ", "") != "")
                                {
                                    out1.MsgOutContent = objServiceDetail.Content1;
                                    insertedCnt++;
                                }

                                if (i == 2 && objServiceDetail.Content1 != null && objServiceDetail.Content2.Replace(" ", "") != "")
                                {
                                    out1.MsgOutContent = objServiceDetail.Content2;
                                    insertedCnt++;
                                }

                                if (i == 3 && objServiceDetail.Content1 != null && objServiceDetail.Content3.Replace(" ", "") != "")
                                {
                                    out1.MsgOutContent = objServiceDetail.Content3;
                                    insertedCnt++;
                                }

                                if (i == 4 && objServiceDetail.Content1 != null && objServiceDetail.Content4.Replace(" ", "") != "")
                                {
                                    out1.MsgOutContent = objServiceDetail.Content4;
                                    insertedCnt++;
                                }

                                if (i == 5 && objServiceDetail.Content1 != null && objServiceDetail.Content5.Replace(" ", "") != "")
                                {
                                    out1.MsgOutContent = objServiceDetail.Content5;
                                    insertedCnt++;
                                }

                                if ((out1.MsgOutContent.Replace(" ", "") == "") && (insertedCnt <= objServiceDetail.NumOfMsg)) continue;
                                
                                out1.RequestTime = objMsgIn84.RequestTime;
                                out1.ResponseTime = DateTime.Now;
                                out1.RequestID = objMsgIn84.RequestID;
                                out1.MsgInContent = strMessage;
                                out1.ContentType = objServiceDetail.ContentType;
                                out1.MsgInID = objMsgIn84.MsgInID;
                                out1.ServiceID = objMsgIn84.ServiceID;
                                out1.MsgIndex = insertedCnt;
                                out1.MsgTotal = objServiceDetail.NumOfMsg;
                                out1.CommandCode = objServiceDetail.Cmd1;
                                out1.IsMore = (insertedCnt == objServiceDetail.NumOfMsg) ? (byte)0 : (byte)1;
                                out1.MsgType = (insertedCnt == objServiceDetail.NumOfMsg) ? (byte)1 : (byte)0;
                                out1.IsCDR = 0;
                                out1.Status = 0;

                                MsgOut_84Manager.Insert(out1);
                                out1 = null;
                            }                            
                        }
                        else
                        {
                            // Wrong ServiceID - Move to Fix
                            flagIn = true;
                        }
                    }
                    else if (CodeManager.GetCodeDetail("MESSAGE", "ERROR_STATUS").CodeValue == "1")
                    {
                        flagIn = true;
                    }
                    else
                    {
                        // Return wrong sms
                        string strWrongService = "";

                        strWrongService = CodeManager.GetContent("WRONGMSG", "0");
                        if (strWrongService != "")
                        {
                            out1.RequestID = objMsgIn84.RequestID;
                            out1.RequestTime = objMsgIn84.RequestTime;
                            out1.ResponseTime = DateTime.Now;
                            out1.MsgInContent = strMessage;
                            out1.MsgInID = objMsgIn84.MsgInID;
                            out1.MobileNo = objMsgIn84.MobileNo;
                            out1.RequestID = objMsgIn84.RequestID;
                            out1.ReceiverMobileNo = "";
                            out1.ServiceID = objMsgIn84.ServiceID;
                            out1.MsgOutContent = strWrongService;
                            out1.ContentType = 0;
                            out1.MsgType = 1;
                            out1.MsgIndex = 1;
                            out1.MsgTotal = 1;
                            out1.IsMore = 0;
                            out1.IsCDR = 0;
                            out1.CommandCode = objMsgIn84.MsgContent;
                            out1.Status = 0;
                            MsgOut_84Manager.Insert(out1);
                            out1 = null;
                        }
                    }
                    objServiceDetail = null;
                }

                if (flagIn)
                    MsgIn_84Manager.UpdateStatus(objMsgIn84.MsgInID, 4);

                else
                    MsgIn_84Manager.UpdateStatus(objMsgIn84.MsgInID, 9);

                return true;
            }
            catch
            {
                MsgIn_84Manager.UpdateStatus(objMsgIn84.MsgInID, 3);
                return false;
            }
        }

        #endregion Process MO 84XX

        #region Process MO 85XX

        public static bool ProcessMessageMO_85(MsgIn_85Info objMsgIn85)
        {
            string strMessage = "";
            string str = "";
            bool flagIn = false;
            MsgOut_85Info out1 = new MsgOut_85Info();

            try
            {
                string ctrlChar = "_";
                if (objMsgIn85 != null)
                {
                    // Format MessageIn 85XX with "_"
                    strMessage = Utility.TrimAll(objMsgIn85.MsgContent.ToLower().Trim().Replace(".", "").Replace("/", ""), " ", ctrlChar);

                    #region Process MsgIn to Partner - 85
                    try
                    {
                        string keyP = Util.GetAppSettings("PartnerAllServiceKey") != null ? Util.GetAppSettings("PartnerAllServiceKey").ToString().ToLower().Trim() : "";

                        string tempP = "";
                        if (objMsgIn85.MsgContent.IndexOf(" ") > 0)
                            tempP = objMsgIn85.MsgContent.Substring(0, objMsgIn85.MsgContent.IndexOf(" ")).ToLower().Trim();
                        else
                            tempP = objMsgIn85.MsgContent.ToLower();

                        if (keyP.Contains(tempP))
                        {
                            PartnerProcess.ProcessSendMO(objMsgIn85.MsgInID, objMsgIn85.RequestID, objMsgIn85.MobileNo, objMsgIn85.ServiceID, objMsgIn85.MsgContent, tempP , objMsgIn85.RequestTime.ToString());

                            return true;
                        }
                    }
                    catch { return false; }
                    #endregion Process MsgIn to Partner - 85

                    int length = strMessage.LastIndexOf("_");
                    if (length > 0)
                    {
                        string str1 = strMessage.Substring(length + 1).Replace("O", "0").Replace("o", "0");
                        try
                        {
                            if (Validators.IsPhoneNumber(str1, PhoneNumberTypes.VN))
                            {
                                str = Validators.FormatMobileNo(str1);
                                strMessage = strMessage.Substring(0, length);
                            }
                        }
                        catch { }
                    }
                    ServiceDetailsInfo objServiceDetail = ServiceDetailsManager.VerifyForMsgOut_85(strMessage, objMsgIn85);
                    if (objServiceDetail != null)
                    {
                        if (objServiceDetail.ServiceID.Trim() != "8678" && objServiceDetail.ServiceID.Trim() != "8778")
                        {
                            // Normal message
                            if (objServiceDetail.NumOfMsg > 5) objServiceDetail.NumOfMsg = 5;

                            byte insertedCnt = 0;
                            for (byte i = 1; i <= 5; i++)
                            {
                                out1 = new MsgOut_85Info();

                                // Message 1st - Nst
                                out1.MobileNo = objMsgIn85.MobileNo;
                                out1.ReceiverMobileNo = "";

                                if (i == 1 && objServiceDetail.Content1 != null && objServiceDetail.Content1.Replace(" ", "") != "")
                                {
                                    out1.MsgOutContent = objServiceDetail.Content1;
                                    insertedCnt++;
                                }

                                if (i == 2 && objServiceDetail.Content1 != null && objServiceDetail.Content2.Replace(" ", "") != "")
                                {
                                    out1.MsgOutContent = objServiceDetail.Content2;
                                    insertedCnt++;
                                }

                                if (i == 3 && objServiceDetail.Content1 != null && objServiceDetail.Content3.Replace(" ", "") != "")
                                {
                                    out1.MsgOutContent = objServiceDetail.Content3;
                                    insertedCnt++;
                                }

                                if (i == 4 && objServiceDetail.Content1 != null && objServiceDetail.Content4.Replace(" ", "") != "")
                                {
                                    out1.MsgOutContent = objServiceDetail.Content4;
                                    insertedCnt++;
                                }

                                if (i == 5 && objServiceDetail.Content1 != null && objServiceDetail.Content5.Replace(" ", "") != "")
                                {
                                    out1.MsgOutContent = objServiceDetail.Content5;
                                    insertedCnt++;
                                }

                                if ((out1.MsgOutContent.Replace(" ", "") == "") && (insertedCnt <= objServiceDetail.NumOfMsg)) continue;
                                
                                out1.RequestTime = objMsgIn85.RequestTime;
                                out1.ResponseTime = DateTime.Now;
                                out1.RequestID = objMsgIn85.RequestID;
                                out1.MsgInContent = strMessage;
                                out1.ContentType = objServiceDetail.ContentType;
                                out1.MsgInID = objMsgIn85.MsgInID;
                                out1.ServiceID = objMsgIn85.ServiceID;
                                out1.MsgIndex = insertedCnt;
                                out1.MsgTotal = objServiceDetail.NumOfMsg;
                                out1.CommandCode = objServiceDetail.Cmd1;
                                out1.IsMore = (insertedCnt == objServiceDetail.NumOfMsg) ? (byte)0 : (byte)1;
                                out1.MsgType = (insertedCnt == objServiceDetail.NumOfMsg) ? (byte)1 : (byte)0;
                                out1.IsCDR = 0;
                                out1.Status = 0;

                                MsgOut_85Manager.Insert(out1);
                                out1 = null;
                            }                            
                        }
                        else
                        {
                            // Wrong ServiceID - Move to Fix
                            flagIn = true;
                        }
                    }
                    else if (CodeManager.GetCodeDetail("MESSAGE", "ERROR_STATUS").CodeValue == "1")
                    {
                        flagIn = true;
                    }
                    else
                    {
                        // Return wrong sms
                        string strWrongService = "";

                        strWrongService = CodeManager.GetContent("WRONGMSG", "0");
                        if (strWrongService != "")
                        {
                            out1.RequestID = objMsgIn85.RequestID;
                            out1.RequestTime = objMsgIn85.RequestTime;
                            out1.ResponseTime = DateTime.Now;
                            out1.MsgInContent = strMessage;
                            out1.MsgInID = objMsgIn85.MsgInID;
                            out1.MobileNo = objMsgIn85.MobileNo;
                            out1.RequestID = objMsgIn85.RequestID;
                            out1.ReceiverMobileNo = "";
                            out1.ServiceID = objMsgIn85.ServiceID;
                            out1.MsgOutContent = strWrongService;
                            out1.ContentType = 0;
                            out1.MsgType = 1;
                            out1.MsgIndex = 1;
                            out1.MsgTotal = 1;
                            out1.IsMore = 0;
                            out1.IsCDR = 0;
                            out1.CommandCode = objMsgIn85.MsgContent;
                            out1.Status = 0;
                            MsgOut_85Manager.Insert(out1);
                            out1 = null;
                        }
                    }
                    objServiceDetail = null;
                }

                if (flagIn)
                    MsgIn_85Manager.UpdateStatus(objMsgIn85.MsgInID, 4);

                else
                    MsgIn_85Manager.UpdateStatus(objMsgIn85.MsgInID, 9);

                return true;
            }
            catch
            {
                MsgIn_85Manager.UpdateStatus(objMsgIn85.MsgInID, 3);
                return false;
            }
        }

        #endregion Process MO 85XX

        #region Process MO 86XX

        public static bool ProcessMessageMO_86(MsgIn_86Info objMsgIn86)
        {
            string strMessage = "";
            string str = "";
            bool flagIn = false;
            MsgOut_86Info out1 = new MsgOut_86Info();

            try
            {
                string ctrlChar = "_";
                if (objMsgIn86 != null)
                {
                    // Format MessageIn 86XX with "_"
                    strMessage = Utility.TrimAll(objMsgIn86.MsgContent.ToLower().Trim().Replace(".", "").Replace("/", ""), " ", ctrlChar);

                    #region Process MsgIn to Partner - 86
                    try
                    {
                        string keyP = Util.GetAppSettings("PartnerAllServiceKey") != null ? Util.GetAppSettings("PartnerAllServiceKey").ToString().ToLower().Trim() : "";

                        string tempP = "";
                        if (objMsgIn86.MsgContent.IndexOf(" ") > 0)
                            tempP = objMsgIn86.MsgContent.Substring(0, objMsgIn86.MsgContent.IndexOf(" ")).ToLower().Trim();
                        else
                            tempP = objMsgIn86.MsgContent.ToLower();

                        if (keyP.Contains(tempP))
                        {
                            PartnerProcess.ProcessSendMO(objMsgIn86.MsgInID, objMsgIn86.RequestID, objMsgIn86.MobileNo, objMsgIn86.ServiceID, objMsgIn86.MsgContent, tempP, objMsgIn86.RequestTime.ToString());

                            return true;
                        }
                    }
                    catch { return false; }
                    #endregion Process MsgIn to Partner - 86

                    int length = strMessage.LastIndexOf("_");
                    if (length > 0)
                    {
                        string str1 = strMessage.Substring(length + 1).Replace("O", "0").Replace("o", "0");
                        try
                        {
                            if (Validators.IsPhoneNumber(str1, PhoneNumberTypes.VN))
                            {
                                str = Validators.FormatMobileNo(str1);
                                strMessage = strMessage.Substring(0, length);
                            }
                        }
                        catch { }
                    }
                    ServiceDetailsInfo objServiceDetail = ServiceDetailsManager.VerifyForMsgOut_86(strMessage, objMsgIn86);
                    if (objServiceDetail != null)
                    {
                        if (int.Parse(objMsgIn86.ServiceID) >= int.Parse(objServiceDetail.ServiceID))
                        {
                            // Normal message
                            if (objServiceDetail.NumOfMsg > 5) objServiceDetail.NumOfMsg = 5;

                            byte insertedCnt = 0;
                            for (byte i = 1; i <= 5; i++)
                            {
                                out1 = new MsgOut_86Info();

                                // Message 1st - Nst
                                out1.MobileNo = objMsgIn86.MobileNo;
                                out1.ReceiverMobileNo = "";

                                if (i == 1 && objServiceDetail.Content1 != null && objServiceDetail.Content1.Replace(" ", "") != "")
                                {
                                    out1.MsgOutContent = objServiceDetail.Content1;
                                    insertedCnt++;
                                }

                                if (i == 2 && objServiceDetail.Content1 != null && objServiceDetail.Content2.Replace(" ", "") != "")
                                {
                                    out1.MsgOutContent = objServiceDetail.Content2;
                                    insertedCnt++;
                                }

                                if (i == 3 && objServiceDetail.Content1 != null && objServiceDetail.Content3.Replace(" ", "") != "")
                                {
                                    out1.MsgOutContent = objServiceDetail.Content3;
                                    insertedCnt++;
                                }

                                if (i == 4 && objServiceDetail.Content1 != null && objServiceDetail.Content4.Replace(" ", "") != "")
                                {
                                    out1.MsgOutContent = objServiceDetail.Content4;
                                    insertedCnt++;
                                }

                                if (i == 5 && objServiceDetail.Content1 != null && objServiceDetail.Content5.Replace(" ", "") != "")
                                {
                                    out1.MsgOutContent = objServiceDetail.Content5;
                                    insertedCnt++;
                                }

                                if ((out1.MsgOutContent.Replace(" ", "") == "") && (insertedCnt <= objServiceDetail.NumOfMsg)) continue;
                                
                                out1.RequestTime = objMsgIn86.RequestTime;
                                out1.ResponseTime = DateTime.Now;
                                out1.RequestID = objMsgIn86.RequestID;
                                out1.MsgInContent = strMessage;
                                out1.ContentType = objServiceDetail.ContentType;
                                out1.MsgInID = objMsgIn86.MsgInID;
                                out1.ServiceID = objMsgIn86.ServiceID;
                                out1.MsgIndex = insertedCnt;
                                out1.MsgTotal = objServiceDetail.NumOfMsg;
                                out1.CommandCode = objServiceDetail.Cmd1;
                                out1.IsMore = (insertedCnt == objServiceDetail.NumOfMsg) ? (byte)0 : (byte)1;
                                out1.MsgType = (insertedCnt == objServiceDetail.NumOfMsg) ? (byte)1 : (byte)0;
                                out1.IsCDR = 0;
                                out1.Status = 0;

                                MsgOut_86Manager.Insert(out1);
                                out1 = null;
                            }                            
                        }
                        else
                        {
                            // Wrong ServiceID - Move to Fix
                            flagIn = true;
                        }
                    }
                    else if (CodeManager.GetCodeDetail("MESSAGE", "ERROR_STATUS").CodeValue == "1")
                    {
                        flagIn = true;
                    }
                    else
                    {
                        // Return wrong sms
                        string strWrongService = "";

                        strWrongService = CodeManager.GetContent("WRONGMSG", "0");
                        if (strWrongService != "")
                        {
                            out1.RequestID = objMsgIn86.RequestID;
                            out1.RequestTime = objMsgIn86.RequestTime;
                            out1.ResponseTime = DateTime.Now;
                            out1.MsgInContent = strMessage;
                            out1.MsgInID = objMsgIn86.MsgInID;
                            out1.MobileNo = objMsgIn86.MobileNo;
                            out1.RequestID = objMsgIn86.RequestID;
                            out1.ReceiverMobileNo = "";
                            out1.ServiceID = objMsgIn86.ServiceID;
                            out1.MsgOutContent = strWrongService;
                            out1.ContentType = 0;
                            out1.MsgType = 1;
                            out1.MsgIndex = 1;
                            out1.MsgTotal = 1;
                            out1.IsMore = 0;
                            out1.IsCDR = 0;
                            out1.CommandCode = objMsgIn86.MsgContent;
                            out1.Status = 0;
                            MsgOut_86Manager.Insert(out1);
                            out1 = null;
                        }
                    }
                    objServiceDetail = null;
                }

                if (flagIn)
                    MsgIn_86Manager.UpdateStatus(objMsgIn86.MsgInID, 4);

                else
                    MsgIn_86Manager.UpdateStatus(objMsgIn86.MsgInID, 9);

                return true;
            }
            catch
            {
                MsgIn_86Manager.UpdateStatus(objMsgIn86.MsgInID, 3);
                return false;
            }
        }

        #endregion Process MO 86XX

        #region Process MO 87XX

        public static bool ProcessMessageMO_87(MsgIn_87Info objMsgIn87)
        {
            string strMessage = "";
            string str = "";
            bool flagIn = false;
            MsgOut_87Info out1 = new MsgOut_87Info();

            try
            {
                string ctrlChar = "_";
                if (objMsgIn87 != null)
                {
                    // Format MessageIn 87XX with "_"
                    strMessage = Utility.TrimAll(objMsgIn87.MsgContent.ToLower().Trim().Replace(".", "").Replace("/", ""), " ", ctrlChar);

                    #region Process MsgIn to Partner - 87
                    try
                    {
                        string keyP = Util.GetAppSettings("PartnerAllServiceKey") != null ? Util.GetAppSettings("PartnerAllServiceKey").ToString().ToLower().Trim() : "";

                        string tempP = "";
                        if (objMsgIn87.MsgContent.IndexOf(" ") > 0)
                            tempP = objMsgIn87.MsgContent.Substring(0, objMsgIn87.MsgContent.IndexOf(" ")).ToLower().Trim();
                        else
                            tempP = objMsgIn87.MsgContent.ToLower();

                        if (keyP.Contains(tempP))
                        {
                            PartnerProcess.ProcessSendMO(objMsgIn87.MsgInID, objMsgIn87.RequestID, objMsgIn87.MobileNo, objMsgIn87.ServiceID, objMsgIn87.MsgContent, tempP, objMsgIn87.RequestTime.ToString());

                            return true;
                        }
                    }
                    catch { return false; }
                    #endregion Process MsgIn to Partner - 87

                    int length = strMessage.LastIndexOf("_");
                    if (length > 0)
                    {
                        string str1 = strMessage.Substring(length + 1).Replace("O", "0").Replace("o", "0");
                        try
                        {
                            if (Validators.IsPhoneNumber(str1, PhoneNumberTypes.VN))
                            {
                                str = Validators.FormatMobileNo(str1);
                                strMessage = strMessage.Substring(0, length);
                            }
                        }
                        catch { }
                    }
                    ServiceDetailsInfo objServiceDetail = ServiceDetailsManager.VerifyForMsgOut_87(strMessage, objMsgIn87);
                    if (objServiceDetail != null)
                    {
                        // Normal message
                        if (objServiceDetail.NumOfMsg > 5) objServiceDetail.NumOfMsg = 5;

                        byte insertedCnt = 0;
                        for (byte i = 1; i <= 5; i++)
                        {
                            out1 = new MsgOut_87Info();                                

                            // Message 1st - Nst
                            out1.MobileNo = objMsgIn87.MobileNo;
                            out1.ReceiverMobileNo = "";

                            if (i == 1 && objServiceDetail.Content1 != null && objServiceDetail.Content1.Replace(" ", "") != "")
                            {
                                out1.MsgOutContent = objServiceDetail.Content1;
                                insertedCnt++;
                            }

                            if (i == 2 && objServiceDetail.Content1 != null && objServiceDetail.Content2.Replace(" ", "") != "")
                            {
                                out1.MsgOutContent = objServiceDetail.Content2;
                                insertedCnt++;
                            }

                            if (i == 3 && objServiceDetail.Content1 != null && objServiceDetail.Content3.Replace(" ", "") != "")
                            {
                                out1.MsgOutContent = objServiceDetail.Content3;
                                insertedCnt++;
                            }

                            if (i == 4 && objServiceDetail.Content1 != null && objServiceDetail.Content4.Replace(" ", "") != "")
                            {
                                out1.MsgOutContent = objServiceDetail.Content4;
                                insertedCnt++;
                            }

                            if (i == 5 && objServiceDetail.Content1 != null && objServiceDetail.Content5.Replace(" ", "") != "")
                            {
                                out1.MsgOutContent = objServiceDetail.Content5;
                                insertedCnt++;
                            }

                            if ((out1.MsgOutContent.Replace(" ", "") == "") && (insertedCnt <= objServiceDetail.NumOfMsg)) continue;
                            
                            out1.RequestTime = objMsgIn87.RequestTime;
                            out1.ResponseTime = DateTime.Now;
                            out1.RequestID = objMsgIn87.RequestID;
                            out1.MsgInContent = strMessage;
                            out1.ContentType = objServiceDetail.ContentType;
                            out1.MsgInID = objMsgIn87.MsgInID;
                            out1.ServiceID = objMsgIn87.ServiceID;
                            out1.MsgIndex = insertedCnt;
                            out1.MsgTotal = objServiceDetail.NumOfMsg;
                            out1.CommandCode = objServiceDetail.Cmd1;
                            out1.IsMore = (insertedCnt == objServiceDetail.NumOfMsg) ? (byte)0 : (byte)1;
                            out1.MsgType = (insertedCnt == objServiceDetail.NumOfMsg) ? (byte)1 : (byte)0;
                            out1.IsCDR = 0;
                            out1.Status = 0;

                            MsgOut_87Manager.Insert(out1);
                            out1 = null;
                        }                            
                    }
                    else if (CodeManager.GetCodeDetail("MESSAGE", "ERROR_STATUS").CodeValue == "1")
                    {
                        flagIn = true;
                    }
                    else
                    {
                        // Return wrong sms
                        string strWrongService = "";

                        strWrongService = CodeManager.GetContent("WRONGMSG", "0");
                        if (strWrongService != "")
                        {
                            out1.RequestID = objMsgIn87.RequestID;
                            out1.RequestTime = objMsgIn87.RequestTime;
                            out1.ResponseTime = DateTime.Now;
                            out1.MsgInContent = strMessage;
                            out1.MsgInID = objMsgIn87.MsgInID;
                            out1.MobileNo = objMsgIn87.MobileNo;
                            out1.RequestID = objMsgIn87.RequestID;
                            out1.ReceiverMobileNo = "";
                            out1.ServiceID = objMsgIn87.ServiceID;
                            out1.MsgOutContent = strWrongService;
                            out1.ContentType = 0;
                            out1.MsgType = 1;
                            out1.MsgIndex = 1;
                            out1.MsgTotal = 1;
                            out1.IsMore = 0;
                            out1.IsCDR = 0;
                            out1.CommandCode = objMsgIn87.MsgContent;
                            out1.Status = 0;
                            MsgOut_87Manager.Insert(out1);
                            out1 = null;
                        }
                    }
                    objServiceDetail = null;
                }

                if (flagIn)
                    MsgIn_87Manager.UpdateStatus(objMsgIn87.MsgInID, 4);

                else
                    MsgIn_87Manager.UpdateStatus(objMsgIn87.MsgInID, 9);

                return true;
            }
            catch
            {
                MsgIn_87Manager.UpdateStatus(objMsgIn87.MsgInID, 3);
                return false;
            }
        }

        #endregion Process MO 87XX

        #endregion MsgIn Process

        #region  MsgOut Process

        #region Process MT 81XX

        public static bool ProcessMessageMT_81(MsgOut_81Info objMsgOut81)
        {
            string msgOutContents = "";
            MsgOut_81Info @out = null;
            bool flag1 = false;
            try
            {
                if (objMsgOut81 != null)
                {
                    @out = objMsgOut81;
                    byte[] bytes = new ASCIIEncoding().GetBytes(objMsgOut81.MsgOutContent.ToString());
                    if (objMsgOut81.ContentType == 0)
                        msgOutContents = Convert.ToBase64String(bytes);
                    
                    else
                        msgOutContents = objMsgOut81.MsgOutContent;
                    
                    string MobileNo = objMsgOut81.MobileNo;
                    string Content = msgOutContents;
                    string ServiceID = objMsgOut81.ServiceID;
                    string CommandCode = objMsgOut81.CommandCode;
                    string MessageType = objMsgOut81.MsgType.ToString();
                    string RequestID = objMsgOut81.RequestID.ToString();
                    string TotalMsg = objMsgOut81.MsgTotal.ToString();
                    string Index = objMsgOut81.MsgIndex.ToString();
                    string IsMore = objMsgOut81.IsMore.ToString();
                    string ContentType = objMsgOut81.ContentType.ToString();
                }
                flag1 = true;
            }
            catch
            {
                flag1 = false;
            }
            return flag1;
        }

        #endregion

        #region Process MT 82XX

        public static bool ProcessMessageMT_82(MsgOut_82Info objMsgOut82)
        {
            string msgOutContents = "";
            MsgOut_82Info @out = null;
            bool flag1 = false;
            try
            {
                if (objMsgOut82 != null)
                {
                    @out = objMsgOut82;
                    byte[] bytes = new ASCIIEncoding().GetBytes(objMsgOut82.MsgOutContent.ToString());
                    if (objMsgOut82.ContentType == 0)
                        msgOutContents = Convert.ToBase64String(bytes);

                    else
                        msgOutContents = objMsgOut82.MsgOutContent;

                    string MobileNo = objMsgOut82.MobileNo;
                    string Content = msgOutContents;
                    string ServiceID = objMsgOut82.ServiceID;
                    string CommandCode = objMsgOut82.CommandCode;
                    string MessageType = objMsgOut82.MsgType.ToString();
                    string RequestID = objMsgOut82.RequestID.ToString();
                    string TotalMsg = objMsgOut82.MsgTotal.ToString();
                    string Index = objMsgOut82.MsgIndex.ToString();
                    string IsMore = objMsgOut82.IsMore.ToString();
                    string ContentType = objMsgOut82.ContentType.ToString();
                }
                flag1 = true;
            }
            catch
            {
                flag1 = false;
            }
            return flag1;
        }

        #endregion

        #region Process MT 83XX

        public static bool ProcessMessageMT_83(MsgOut_83Info objMsgOut83)
        {
            string msgOutContents = "";
            MsgOut_83Info @out = null;
            bool flag1 = false;
            try
            {
                if (objMsgOut83 != null)
                {
                    @out = objMsgOut83;
                    byte[] bytes = new ASCIIEncoding().GetBytes(objMsgOut83.MsgOutContent.ToString());
                    if (objMsgOut83.ContentType == 0)
                        msgOutContents = Convert.ToBase64String(bytes);

                    else
                        msgOutContents = objMsgOut83.MsgOutContent;

                    string MobileNo = objMsgOut83.MobileNo;
                    string Content = msgOutContents;
                    string ServiceID = objMsgOut83.ServiceID;
                    string CommandCode = objMsgOut83.CommandCode;
                    string MessageType = objMsgOut83.MsgType.ToString();
                    string RequestID = objMsgOut83.RequestID.ToString();
                    string TotalMsg = objMsgOut83.MsgTotal.ToString();
                    string Index = objMsgOut83.MsgIndex.ToString();
                    string IsMore = objMsgOut83.IsMore.ToString();
                    string ContentType = objMsgOut83.ContentType.ToString();
                }
                flag1 = true;
            }
            catch
            {
                flag1 = false;
            }
            return flag1;
        }

        #endregion

        #region Process MT 84XX

        public static bool ProcessMessageMT_84(MsgOut_84Info objMsgOut84)
        {
            string msgOutContents = "";
            MsgOut_84Info @out = null;
            bool flag1 = false;
            try
            {
                if (objMsgOut84 != null)
                {
                    @out = objMsgOut84;
                    byte[] bytes = new ASCIIEncoding().GetBytes(objMsgOut84.MsgOutContent.ToString());
                    if (objMsgOut84.ContentType == 0)
                        msgOutContents = Convert.ToBase64String(bytes);

                    else
                        msgOutContents = objMsgOut84.MsgOutContent;

                    string MobileNo = objMsgOut84.MobileNo;
                    string Content = msgOutContents;
                    string ServiceID = objMsgOut84.ServiceID;
                    string CommandCode = objMsgOut84.CommandCode;
                    string MessageType = objMsgOut84.MsgType.ToString();
                    string RequestID = objMsgOut84.RequestID.ToString();
                    string TotalMsg = objMsgOut84.MsgTotal.ToString();
                    string Index = objMsgOut84.MsgIndex.ToString();
                    string IsMore = objMsgOut84.IsMore.ToString();
                    string ContentType = objMsgOut84.ContentType.ToString();
                }
                flag1 = true;
            }
            catch
            {
                flag1 = false;
            }
            return flag1;
        }

        #endregion

        #region Process MT 85XX

        public static bool ProcessMessageMT_85(MsgOut_85Info objMsgOut85)
        {
            string msgOutContents = "";
            MsgOut_85Info @out = null;
            bool flag1 = false;
            try
            {
                if (objMsgOut85 != null)
                {
                    @out = objMsgOut85;
                    byte[] bytes = new ASCIIEncoding().GetBytes(objMsgOut85.MsgOutContent.ToString());
                    if (objMsgOut85.ContentType == 0)
                        msgOutContents = Convert.ToBase64String(bytes);

                    else
                        msgOutContents = objMsgOut85.MsgOutContent;

                    string MobileNo = objMsgOut85.MobileNo;
                    string Content = msgOutContents;
                    string ServiceID = objMsgOut85.ServiceID;
                    string CommandCode = objMsgOut85.CommandCode;
                    string MessageType = objMsgOut85.MsgType.ToString();
                    string RequestID = objMsgOut85.RequestID.ToString();
                    string TotalMsg = objMsgOut85.MsgTotal.ToString();
                    string Index = objMsgOut85.MsgIndex.ToString();
                    string IsMore = objMsgOut85.IsMore.ToString();
                    string ContentType = objMsgOut85.ContentType.ToString();
                }
                flag1 = true;
            }
            catch
            {
                flag1 = false;
            }
            return flag1;
        }

        #endregion

        #region Process MT 86XX

        public static bool ProcessMessageMT_86(MsgOut_86Info objMsgOut86)
        {
            string msgOutContents = "";
            MsgOut_86Info @out = null;
            bool flag1 = false;
            try
            {
                if (objMsgOut86 != null)
                {
                    @out = objMsgOut86;
                    byte[] bytes = new ASCIIEncoding().GetBytes(objMsgOut86.MsgOutContent.ToString());
                    if (objMsgOut86.ContentType == 0)
                        msgOutContents = Convert.ToBase64String(bytes);

                    else
                        msgOutContents = objMsgOut86.MsgOutContent;

                    string MobileNo = objMsgOut86.MobileNo;
                    string Content = msgOutContents;
                    string ServiceID = objMsgOut86.ServiceID;
                    string CommandCode = objMsgOut86.CommandCode;
                    string MessageType = objMsgOut86.MsgType.ToString();
                    string RequestID = objMsgOut86.RequestID.ToString();
                    string TotalMsg = objMsgOut86.MsgTotal.ToString();
                    string Index = objMsgOut86.MsgIndex.ToString();
                    string IsMore = objMsgOut86.IsMore.ToString();
                    string ContentType = objMsgOut86.ContentType.ToString();
                }
                flag1 = true;
            }
            catch
            {
                flag1 = false;
            }
            return flag1;
        }

        #endregion

        #region Process MT 87XX

        public static bool ProcessMessageMT_87(MsgOut_87Info objMsgOut87)
        {
            string msgOutContents = "";
            MsgOut_87Info @out = null;
            bool flag1 = false;
            try
            {
                if (objMsgOut87 != null)
                {
                    @out = objMsgOut87;
                    byte[] bytes = new ASCIIEncoding().GetBytes(objMsgOut87.MsgOutContent.ToString());
                    if (objMsgOut87.ContentType == 0)
                        msgOutContents = Convert.ToBase64String(bytes);

                    else
                        msgOutContents = objMsgOut87.MsgOutContent;

                    string MobileNo = objMsgOut87.MobileNo;
                    string Content = msgOutContents;
                    string ServiceID = objMsgOut87.ServiceID;
                    string CommandCode = objMsgOut87.CommandCode;
                    string MessageType = objMsgOut87.MsgType.ToString();
                    string RequestID = objMsgOut87.RequestID.ToString();
                    string TotalMsg = objMsgOut87.MsgTotal.ToString();
                    string Index = objMsgOut87.MsgIndex.ToString();
                    string IsMore = objMsgOut87.IsMore.ToString();
                    string ContentType = objMsgOut87.ContentType.ToString();
                }
                flag1 = true;
            }
            catch
            {
                flag1 = false;
            }
            return flag1;
        }

        #endregion

        #region Process MT 80XX

        public static bool ProcessMessageMT_Wait(MsgOut_80Info objMsgOut80)
        {
            string msgOutContents = "";
            MsgOut_80Info @out = null;
            bool flag1 = false;
            try
            {
                if (objMsgOut80 != null)
                {
                    @out = objMsgOut80;
                    byte[] bytes = new ASCIIEncoding().GetBytes(objMsgOut80.MsgOutContent.ToString());
                    if (objMsgOut80.ContentType == 0)
                        msgOutContents = Convert.ToBase64String(bytes);

                    else
                        msgOutContents = objMsgOut80.MsgOutContent;

                    string MobileNo = objMsgOut80.MobileNo;
                    string Content = msgOutContents;
                    string ServiceID = objMsgOut80.ServiceID;
                    string CommandCode = objMsgOut80.CommandCode;
                    string MessageType = objMsgOut80.MsgType.ToString();
                    string RequestID = objMsgOut80.RequestID.ToString();
                    string TotalMsg = objMsgOut80.MsgTotal.ToString();
                    string Index = objMsgOut80.MsgIndex.ToString();
                    string IsMore = objMsgOut80.IsMore.ToString();
                    string ContentType = objMsgOut80.ContentType.ToString();
                }
                flag1 = true;
            }
            catch
            {
                flag1 = false;
            }
            return flag1;
        }

        #endregion Process MT 80XX

        #endregion MsgOut Process

       
    }
}